
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Policies &#8212; Python 3.9.6 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.9.6 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="Platform Support" href="asyncio-platforms.html" />
    <link rel="prev" title="Transports and Protocols" href="asyncio-protocol.html" />
    <link rel="canonical" href="https://docs.python.org/3/library/asyncio-policy.html" />
    
      
      
    

    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>

    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
     


  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="asyncio-platforms.html" title="Platform Support"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="asyncio-protocol.html" title="Transports and Protocols"
             accesskey="P">previous</a> |</li>

    <li><img src="../_static/py.png" alt=""
             style="vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://www.python.org/">Python</a> &#187;</li>
    

    <li>
      <a href="../index.html">3.9.6 Documentation</a> &#187;
    </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="policies">
<span id="asyncio-policies"></span><h1>Policies<a class="headerlink" href="#policies" title="Permalink to this headline">¶</a></h1>
<p>An event loop policy is a global per-process object that controls
the management of the event loop. Each event loop has a default
policy, which can be changed and customized using the policy API.</p>
<p>A policy defines the notion of <em>context</em> and manages a
separate event loop per context. The default policy
defines <em>context</em> to be the current thread.</p>
<p>By using a custom event loop policy, the behavior of
<a class="reference internal" href="asyncio-eventloop.html#asyncio.get_event_loop" title="asyncio.get_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_event_loop()</span></code></a>, <a class="reference internal" href="asyncio-eventloop.html#asyncio.set_event_loop" title="asyncio.set_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_event_loop()</span></code></a>, and
<a class="reference internal" href="asyncio-eventloop.html#asyncio.new_event_loop" title="asyncio.new_event_loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">new_event_loop()</span></code></a> functions can be customized.</p>
<p>Policy objects should implement the APIs defined
in the <a class="reference internal" href="#asyncio.AbstractEventLoopPolicy" title="asyncio.AbstractEventLoopPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbstractEventLoopPolicy</span></code></a> abstract base class.</p>
<section id="getting-and-setting-the-policy">
<h2>Getting and Setting the Policy<a class="headerlink" href="#getting-and-setting-the-policy" title="Permalink to this headline">¶</a></h2>
<p>The following functions can be used to get and set the policy
for the current process:</p>
<dl class="function">
<dt id="asyncio.get_event_loop_policy">
<code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">get_event_loop_policy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.get_event_loop_policy" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current process-wide policy.</p>
</dd></dl>

<dl class="function">
<dt id="asyncio.set_event_loop_policy">
<code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">set_event_loop_policy</code><span class="sig-paren">(</span><em class="sig-param">policy</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.set_event_loop_policy" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the current process-wide policy to <em>policy</em>.</p>
<p>If <em>policy</em> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, the default policy is restored.</p>
</dd></dl>

</section>
<section id="policy-objects">
<h2>Policy Objects<a class="headerlink" href="#policy-objects" title="Permalink to this headline">¶</a></h2>
<p>The abstract event loop policy base class is defined as follows:</p>
<dl class="class">
<dt id="asyncio.AbstractEventLoopPolicy">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">AbstractEventLoopPolicy</code><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract base class for asyncio policies.</p>
<dl class="method">
<dt id="asyncio.AbstractEventLoopPolicy.get_event_loop">
<code class="sig-name descname">get_event_loop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy.get_event_loop" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the event loop for the current context.</p>
<p>Return an event loop object implementing the
<a class="reference internal" href="asyncio-eventloop.html#asyncio.AbstractEventLoop" title="asyncio.AbstractEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbstractEventLoop</span></code></a> interface.</p>
<p>This method should never return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.6.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractEventLoopPolicy.set_event_loop">
<code class="sig-name descname">set_event_loop</code><span class="sig-paren">(</span><em class="sig-param">loop</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy.set_event_loop" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the event loop for the current context to <em>loop</em>.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractEventLoopPolicy.new_event_loop">
<code class="sig-name descname">new_event_loop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy.new_event_loop" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and return a new event loop object.</p>
<p>This method should never return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractEventLoopPolicy.get_child_watcher">
<code class="sig-name descname">get_child_watcher</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy.get_child_watcher" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a child process watcher object.</p>
<p>Return a watcher object implementing the
<a class="reference internal" href="#asyncio.AbstractChildWatcher" title="asyncio.AbstractChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbstractChildWatcher</span></code></a> interface.</p>
<p>This function is Unix specific.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractEventLoopPolicy.set_child_watcher">
<code class="sig-name descname">set_child_watcher</code><span class="sig-paren">(</span><em class="sig-param">watcher</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractEventLoopPolicy.set_child_watcher" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the current child process watcher to <em>watcher</em>.</p>
<p>This function is Unix specific.</p>
</dd></dl>

</dd></dl>

<p>asyncio ships with the following built-in policies:</p>
<dl class="class">
<dt id="asyncio.DefaultEventLoopPolicy">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">DefaultEventLoopPolicy</code><a class="headerlink" href="#asyncio.DefaultEventLoopPolicy" title="Permalink to this definition">¶</a></dt>
<dd><p>The default asyncio policy.  Uses <a class="reference internal" href="asyncio-eventloop.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a>
on Unix and <a class="reference internal" href="asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> on Windows.</p>
<p>There is no need to install the default policy manually. asyncio
is configured to use the default policy automatically.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, <a class="reference internal" href="asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> is now used by default.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="asyncio.WindowsSelectorEventLoopPolicy">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">WindowsSelectorEventLoopPolicy</code><a class="headerlink" href="#asyncio.WindowsSelectorEventLoopPolicy" title="Permalink to this definition">¶</a></dt>
<dd><p>An alternative event loop policy that uses the
<a class="reference internal" href="asyncio-eventloop.html#asyncio.SelectorEventLoop" title="asyncio.SelectorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorEventLoop</span></code></a> event loop implementation.</p>
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
</dd></dl>

<dl class="class">
<dt id="asyncio.WindowsProactorEventLoopPolicy">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">WindowsProactorEventLoopPolicy</code><a class="headerlink" href="#asyncio.WindowsProactorEventLoopPolicy" title="Permalink to this definition">¶</a></dt>
<dd><p>An alternative event loop policy that uses the
<a class="reference internal" href="asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> event loop implementation.</p>
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
</dd></dl>

</section>
<section id="process-watchers">
<span id="asyncio-watchers"></span><h2>Process Watchers<a class="headerlink" href="#process-watchers" title="Permalink to this headline">¶</a></h2>
<p>A process watcher allows customization of how an event loop monitors
child processes on Unix. Specifically, the event loop needs to know
when a child process has exited.</p>
<p>In asyncio, child processes are created with
<a class="reference internal" href="asyncio-subprocess.html#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_subprocess_exec()</span></code></a> and <a class="reference internal" href="asyncio-eventloop.html#asyncio.loop.subprocess_exec" title="asyncio.loop.subprocess_exec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.subprocess_exec()</span></code></a>
functions.</p>
<p>asyncio defines the <a class="reference internal" href="#asyncio.AbstractChildWatcher" title="asyncio.AbstractChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbstractChildWatcher</span></code></a> abstract base class, which child
watchers should implement, and has four different implementations:
<a class="reference internal" href="#asyncio.ThreadedChildWatcher" title="asyncio.ThreadedChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadedChildWatcher</span></code></a> (configured to be used by default),
<a class="reference internal" href="#asyncio.MultiLoopChildWatcher" title="asyncio.MultiLoopChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiLoopChildWatcher</span></code></a>, <a class="reference internal" href="#asyncio.SafeChildWatcher" title="asyncio.SafeChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">SafeChildWatcher</span></code></a>, and
<a class="reference internal" href="#asyncio.FastChildWatcher" title="asyncio.FastChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">FastChildWatcher</span></code></a>.</p>
<p>See also the <a class="reference internal" href="asyncio-subprocess.html#asyncio-subprocess-threads"><span class="std std-ref">Subprocess and Threads</span></a>
section.</p>
<p>The following two functions can be used to customize the child process watcher
implementation used by the asyncio event loop:</p>
<dl class="function">
<dt id="asyncio.get_child_watcher">
<code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">get_child_watcher</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.get_child_watcher" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current child watcher for the current policy.</p>
</dd></dl>

<dl class="function">
<dt id="asyncio.set_child_watcher">
<code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">set_child_watcher</code><span class="sig-paren">(</span><em class="sig-param">watcher</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.set_child_watcher" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the current child watcher to <em>watcher</em> for the current
policy.  <em>watcher</em> must implement methods defined in the
<a class="reference internal" href="#asyncio.AbstractChildWatcher" title="asyncio.AbstractChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbstractChildWatcher</span></code></a> base class.</p>
</dd></dl>

<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Third-party event loops implementations might not support
custom child watchers.  For such event loops, using
<a class="reference internal" href="#asyncio.set_child_watcher" title="asyncio.set_child_watcher"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_child_watcher()</span></code></a> might be prohibited or have no effect.</p>
</div>
<dl class="class">
<dt id="asyncio.AbstractChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">AbstractChildWatcher</code><a class="headerlink" href="#asyncio.AbstractChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><dl class="method">
<dt id="asyncio.AbstractChildWatcher.add_child_handler">
<code class="sig-name descname">add_child_handler</code><span class="sig-paren">(</span><em class="sig-param">pid</em>, <em class="sig-param">callback</em>, <em class="sig-param">*args</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractChildWatcher.add_child_handler" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a new child handler.</p>
<p>Arrange for <code class="docutils literal notranslate"><span class="pre">callback(pid,</span> <span class="pre">returncode,</span> <span class="pre">*args)</span></code> to be called
when a process with PID equal to <em>pid</em> terminates.  Specifying
another callback for the same process replaces the previous
handler.</p>
<p>The <em>callback</em> callable must be thread-safe.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractChildWatcher.remove_child_handler">
<code class="sig-name descname">remove_child_handler</code><span class="sig-paren">(</span><em class="sig-param">pid</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractChildWatcher.remove_child_handler" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the handler for process with PID equal to <em>pid</em>.</p>
<p>The function returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the handler was successfully
removed, <code class="docutils literal notranslate"><span class="pre">False</span></code> if there was nothing to remove.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractChildWatcher.attach_loop">
<code class="sig-name descname">attach_loop</code><span class="sig-paren">(</span><em class="sig-param">loop</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractChildWatcher.attach_loop" title="Permalink to this definition">¶</a></dt>
<dd><p>Attach the watcher to an event loop.</p>
<p>If the watcher was previously attached to an event loop, then
it is first detached before attaching to the new loop.</p>
<p>Note: loop may be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractChildWatcher.is_active">
<code class="sig-name descname">is_active</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractChildWatcher.is_active" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the watcher is ready to use.</p>
<p>Spawning a subprocess with <em>inactive</em> current child watcher raises
<a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.AbstractChildWatcher.close">
<code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncio.AbstractChildWatcher.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Close the watcher.</p>
<p>This method has to be called to ensure that underlying
resources are cleaned-up.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="asyncio.ThreadedChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">ThreadedChildWatcher</code><a class="headerlink" href="#asyncio.ThreadedChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><p>This implementation starts a new waiting thread for every subprocess spawn.</p>
<p>It works reliably even when the asyncio event loop is run in a non-main OS thread.</p>
<p>There is no noticeable overhead when handling a big number of children (<em>O(1)</em> each
time a child terminates), but starting a thread per process requires extra memory.</p>
<p>This watcher is used by default.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

<dl class="class">
<dt id="asyncio.MultiLoopChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">MultiLoopChildWatcher</code><a class="headerlink" href="#asyncio.MultiLoopChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><p>This implementation registers a <code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> signal handler on
instantiation. That can break third-party code that installs a custom handler for
<code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> signal.</p>
<p>The watcher avoids disrupting other code spawning processes
by polling every process explicitly on a <code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> signal.</p>
<p>There is no limitation for running subprocesses from different threads once the
watcher is installed.</p>
<p>The solution is safe but it has a significant overhead when
handling a big number of processes (<em>O(n)</em> each time a
<code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> is received).</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

<dl class="class">
<dt id="asyncio.SafeChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">SafeChildWatcher</code><a class="headerlink" href="#asyncio.SafeChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><p>This implementation uses active event loop from the main thread to handle
<code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> signal. If the main thread has no running event loop another
thread cannot spawn a subprocess (<a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised).</p>
<p>The watcher avoids disrupting other code spawning processes
by polling every process explicitly on a <code class="xref py py-data docutils literal notranslate"><span class="pre">SIGCHLD</span></code> signal.</p>
<p>This solution is as safe as <a class="reference internal" href="#asyncio.MultiLoopChildWatcher" title="asyncio.MultiLoopChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiLoopChildWatcher</span></code></a> and has the same <em>O(N)</em>
complexity but requires a running event loop in the main thread to work.</p>
</dd></dl>

<dl class="class">
<dt id="asyncio.FastChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">FastChildWatcher</code><a class="headerlink" href="#asyncio.FastChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><p>This implementation reaps every terminated processes by calling
<code class="docutils literal notranslate"><span class="pre">os.waitpid(-1)</span></code> directly, possibly breaking other code spawning
processes and waiting for their termination.</p>
<p>There is no noticeable overhead when handling a big number of
children (<em>O(1)</em> each time a child terminates).</p>
<p>This solution requires a running event loop in the main thread to work, as
<a class="reference internal" href="#asyncio.SafeChildWatcher" title="asyncio.SafeChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">SafeChildWatcher</span></code></a>.</p>
</dd></dl>

<dl class="class">
<dt id="asyncio.PidfdChildWatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncio.</code><code class="sig-name descname">PidfdChildWatcher</code><a class="headerlink" href="#asyncio.PidfdChildWatcher" title="Permalink to this definition">¶</a></dt>
<dd><p>This implementation polls process file descriptors (pidfds) to await child
process termination. In some respects, <a class="reference internal" href="#asyncio.PidfdChildWatcher" title="asyncio.PidfdChildWatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">PidfdChildWatcher</span></code></a> is a
“Goldilocks” child watcher implementation. It doesn’t require signals or
threads, doesn’t interfere with any processes launched outside the event
loop, and scales linearly with the number of subprocesses launched by the
event loop. The main disadvantage is that pidfds are specific to Linux, and
only work on recent (5.3+) kernels.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.9.</span></p>
</div>
</dd></dl>

</section>
<section id="custom-policies">
<h2>Custom Policies<a class="headerlink" href="#custom-policies" title="Permalink to this headline">¶</a></h2>
<p>To implement a new event loop policy, it is recommended to subclass
<a class="reference internal" href="#asyncio.DefaultEventLoopPolicy" title="asyncio.DefaultEventLoopPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">DefaultEventLoopPolicy</span></code></a> and override the methods for which
custom behavior is wanted, e.g.:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyEventLoopPolicy</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">DefaultEventLoopPolicy</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">get_event_loop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get the event loop.</span>

<span class="sd">        This may be None or an instance of EventLoop.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">loop</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
        <span class="c1"># Do something with loop ...</span>
        <span class="k">return</span> <span class="n">loop</span>

<span class="n">asyncio</span><span class="o">.</span><span class="n">set_event_loop_policy</span><span class="p">(</span><span class="n">MyEventLoopPolicy</span><span class="p">())</span>
</pre></div>
</div>
</section>
</section>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Policies</a><ul>
<li><a class="reference internal" href="#getting-and-setting-the-policy">Getting and Setting the Policy</a></li>
<li><a class="reference internal" href="#policy-objects">Policy Objects</a></li>
<li><a class="reference internal" href="#process-watchers">Process Watchers</a></li>
<li><a class="reference internal" href="#custom-policies">Custom Policies</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="asyncio-protocol.html"
                        title="previous chapter">Transports and Protocols</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="asyncio-platforms.html"
                        title="next chapter">Platform Support</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.9/Doc/library/asyncio-policy.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="asyncio-platforms.html" title="Platform Support"
             >next</a> |</li>
        <li class="right" >
          <a href="asyncio-protocol.html" title="Transports and Protocols"
             >previous</a> |</li>

    <li><img src="../_static/py.png" alt=""
             style="vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://www.python.org/">Python</a> &#187;</li>
    

    <li>
      <a href="../index.html">3.9.6 Documentation</a> &#187;
    </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2021, Python Software Foundation.
    <br />

    The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
    <br />

    Last updated on Jun 28, 2021.
    <a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
    <br />

    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.3.1.
    </div>

  </body>
</html>